package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.programming.common.Result;
import com.example.programming.common.tools.BaseController;
import com.example.programming.sys.entity.Ad;
import com.example.programming.sys.mapper.AdMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-06-23
 */
@Api(tags = "ad接口说明")
@RestController
@RequestMapping("/sys/ad")
@CrossOrigin
public class AdController extends BaseController {
    @Autowired
    @Resource
    private AdMapper adMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Ad> page = new Page<Ad>(pageNum, pageSize);
        Ad parms = new Ad();
        QueryWrapper<Ad> warpper = new QueryWrapper<Ad>(parms);

        result.setData(adMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Ad entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            adMapper.insert(entity);
        } else {
            adMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        adMapper.deleteBatchIds(deleteIds);
        return result;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getall")
    public Result ALl() {
        Result result = new Result();
        result.setData(adMapper.selectList(null));
        return result;
    }

    @GetMapping("/hello")
    public String hello() {
        String hello = "hello";
        return hello;
    }


}
